/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MWmi_Win32_DesktopMonitor.h"

//---------------------------------------------------------------------------

MWmi_Win32_DesktopMonitor::MWmi_Win32_DesktopMonitor(void)
{
	propertyCount = 8;
	Initialize();
}

MWmi_Win32_DesktopMonitor::~MWmi_Win32_DesktopMonitor(void)
{
	DeleteChild();
}

//---------------------------------------------------------------------------

MWmiInfo* MWmi_Win32_DesktopMonitor::CreateChild(void)
{
	MWmi_Win32_DesktopMonitor *elem;

	try{
		elem = new MWmi_Win32_DesktopMonitor;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

void MWmi_Win32_DesktopMonitor::DeleteChild(void)
{
	MWmi_Win32_DesktopMonitor *elem;
	
	if(next!=NULL){
		elem = (MWmi_Win32_DesktopMonitor*)next;
		next = NULL;
		delete elem;
	}
}

//---------------------------------------------------------------------------

void MWmi_Win32_DesktopMonitor::Initialize(void)
{
	xDescription           = _T("");
	xMonitorManufacturer   = _T("");
	xMonitorType           = _T("");
	xName                  = _T("");
	xPixelsPerXLogicalInch = 0;
	xPixelsPerYLogicalInch = 0;
	xScreenHeight          = 0;
	xScreenWidth           = 0;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_DesktopMonitor::GetQueryString(void)
{
	CString str;

	str = _T("select * from Win32_DesktopMonitor");
	return str;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_DesktopMonitor::GetPropertyName(int idxIn)
{
	CString str;

	switch(idxIn){
		case 0 :
			str = _T("Description");
			break;
		case 1 :
			str = _T("MonitorManufacturer");
			break;
		case 2 :
			str = _T("MonitorType");
			break;
		case 3 :
			str = _T("Name");
			break;
		case 4 :
			str = _T("PixelsPerXLogicalInch");
			break;
		case 5 :
			str = _T("PixelsPerYLogicalInch");
			break;
		case 6 :
			str = _T("ScreenHeight");
			break;
		case 7 :
			str = _T("ScreenWidth");
			break;

		default :
			str = _T("");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_DesktopMonitor::SetPropertyValue(int idxIn, VARIANT *vtIn)
{
	if(vtIn==NULL) return true;
	if(vtIn->vt==VT_NULL) return true;

	switch(idxIn){
		case 0 :
			if(vtIn->vt!=VT_BSTR) return false;
			xDescription = vtIn->bstrVal;
			break;
		case 1 :
			if(vtIn->vt!=VT_BSTR) return false;
			xMonitorManufacturer = vtIn->bstrVal;
			break;
		case 2 :
			if(vtIn->vt!=VT_BSTR) return false;
			xMonitorType = vtIn->bstrVal;
			break;
		case 3 :
			if(vtIn->vt!=VT_BSTR) return false;
			xName = vtIn->bstrVal;
			break;

		case 4 :
			if(vtIn->vt!=VT_I4) return false;
			xPixelsPerXLogicalInch = vtIn->lVal;
			break;
		case 5 :
			if(vtIn->vt!=VT_I4) return false;
			xPixelsPerYLogicalInch = vtIn->lVal;
			break;
		case 6 :
			if(vtIn->vt!=VT_I4) return false;
			xScreenHeight = vtIn->lVal;
			break;
		case 7 :
			if(vtIn->vt!=VT_I4) return false;
			xScreenWidth = vtIn->lVal;
			break;

		default :
			return false;
	}

	return true;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_DesktopMonitor::SetFromPointer(MWmiInfo* wmiInfoIn)
{
	MWmi_Win32_DesktopMonitor *elem;
	MWmi_Win32_DesktopMonitor *elemNew;

	DeleteChild();
	Initialize();
	if(wmiInfoIn==NULL) return true;

	elem = (MWmi_Win32_DesktopMonitor*)wmiInfoIn;

	xDescription           = elem->xDescription;
	xMonitorManufacturer   = elem->xMonitorManufacturer;
	xMonitorType           = elem->xMonitorType;
	xName                  = elem->xName;
	xPixelsPerXLogicalInch = elem->xPixelsPerXLogicalInch;
	xPixelsPerYLogicalInch = elem->xPixelsPerYLogicalInch;
	xScreenHeight          = elem->xScreenHeight;
	xScreenWidth           = elem->xScreenWidth;


	if(elem->next==NULL) return true;

	try{
		elemNew = new MWmi_Win32_DesktopMonitor;
	}
	catch(...){
		elemNew = NULL;
	}
	if(elemNew==NULL) return false;

	next = elemNew;

	return elemNew->SetFromPointer(elem->next);
}

//---------------------------------------------------------------------------

bool MWmi_Win32_DesktopMonitor::IsEmpty(void)
{
	return xName.IsEmpty() ? true : false;
}

//---------------------------------------------------------------------------
